package com.dingding.order.core.grab.dao.impl;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dao.impl.BaseDaoImpl;
import com.dingding.common.core.beans.Userordert;
import com.dingding.common.core.config.Constants;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.order.core.grab.dao.INoticeUserDao;
import com.dingding.order.core.grab.sql.UserGrabPushSql;
import com.dingding.order.dto.grab.PushCompetitiveOrder;

/**
 * @ClassName: INoticeUserDao
 * @Description: 用户抢单通知实现类(发送短信和推送)
 * @author yuHaiQing
 * @date 2016年6月13日 下午2:58:38
 */
@Component("noticeUserDao")
public class NoticeUserDaoImpl extends BaseDaoImpl<Userordert> implements INoticeUserDao {

	/**
	 * 
	 * @Title: getNoticeContent
	 * @Description: 用户抢单通知接口
	 * @param userType 抢单用户角色
	 * @param placeOrderUserId 下单人Id
	 * @param grabOrderUserId  抢单人Id
	 * @author yuHaiQing
	 * @return PushCompetitiveOrder    返回类型
	 * 
	 */
	@Override
	@Transactional(readOnly = true)
	public PushCompetitiveOrder getNoticeContent(Integer userType, String placeOrderUserId, String grabOrderUserId) {

		String sql = null;
		// 乘客抢单,不需要获取车辆信息
		if (userType == 2) {
			sql = UserGrabPushSql.DRIVER_GRABORDER_GET_USER_INFO;
		} else {
			sql = UserGrabPushSql.PASSAGER_GRABORDER_GET_USER_INFO;
		}

		Object[] userDetalObj = null;
		try {
			userDetalObj = super.sqlQueryUnResultObj(sql, grabOrderUserId, placeOrderUserId);
		} catch (Exception e) {
			log.error("获取用户抢单通知内容时出现异常   类：" + this.getClass() + " getNoticeContent()", e);
		}

		PushCompetitiveOrder pushCompetitiveOrder = new PushCompetitiveOrder();

		if (userDetalObj != null) {
			// 数据转换
			converBean(userDetalObj, pushCompetitiveOrder);
		}
		
		return pushCompetitiveOrder;
	}

	/**
	 * 
	 * @Title: converBean 
	 * @Description:将查询到的数据转换成用户抢单推送bean 
	 * @param userDetalObj 数据库中查询到的数据 
	 * @param pushCompetitiveOrder 推送数据封装bean 
	 */
	private void converBean(Object[] userDetalObj, PushCompetitiveOrder pushCompetitiveOrder) {
		// 下单用户ID
		if (userDetalObj[0] != null) {
			pushCompetitiveOrder.setPublishOrderUserId(userDetalObj[0].toString());
		}
		// 抢单用户头像
		if (userDetalObj[1] != null) {
			pushCompetitiveOrder.setUserAvatar(ToolUtil.imageAddress(userDetalObj[1].toString()));
		}
		// 昵称
		if (userDetalObj[2] != null) {
			pushCompetitiveOrder.setNickName(userDetalObj[2].toString());
		}
		// 手机号
		if (userDetalObj[3] != null) {
			pushCompetitiveOrder.setTellPhone(userDetalObj[3].toString());
		}
		// 设备类型
		if (userDetalObj[4] != null) {
			// 获取设备类型
			String equipmentType = userDetalObj[4].toString();

			if (equipmentType.equals("1")) { // 如果设备类型等于1，表示该设备类型为iOS
				pushCompetitiveOrder.setLoginEquipment("iOS");
			} else if (equipmentType.equals("2")) { // 如果设备类型等于2，表示该设备类型为Android
				pushCompetitiveOrder.setLoginEquipment("Android");
			}
		}
		// 下单人的手机号
		if (userDetalObj[5] != null) {
			pushCompetitiveOrder.setAnotherPhoneNo(userDetalObj[5].toString());
		}
		// 抢单人的头像缩略图
		if (userDetalObj[6] != null) {
			pushCompetitiveOrder.setUserTabloidAvatar(ToolUtil.imageAddress(userDetalObj[6].toString()));
			if(pushCompetitiveOrder.getUserTabloidAvatar() == null){
				pushCompetitiveOrder.setUserTabloidAvatar(Constants.DEFAULT_USER_TABLOID_AVATAR);
			}
		}
		// 抢单人的性别
		if (userDetalObj[7] != null) {
			pushCompetitiveOrder.setSex(ToolUtil.castInteger(userDetalObj[7]));
		}
		// 下单用户别名
		if (userDetalObj[8] != null) {
			pushCompetitiveOrder.setAliasId(userDetalObj[8].toString());
		}
		// 判断数组长度，来确定是否给pushCompetitiveOrder赋车辆信息
		if (userDetalObj.length > 8) {
			// 车辆品牌
			if (userDetalObj[9] != null) {
				pushCompetitiveOrder.setVehicleType(userDetalObj[9].toString());
			}
			// 车辆型号
			if (userDetalObj[10] != null) {
				pushCompetitiveOrder.setVehicleModel(userDetalObj[10].toString());
			}
			// 车辆颜色
			if (userDetalObj[11] != null) {
				pushCompetitiveOrder.setVehicleColor(userDetalObj[11].toString());
			}
			// 车辆照片
			if (userDetalObj[12] != null) {
				pushCompetitiveOrder.setVehicleImage(ToolUtil.imageAddress(userDetalObj[12].toString()));
			}
			// 车牌号
			if (userDetalObj[13] != null) {
				pushCompetitiveOrder.setLicensePlate(userDetalObj[13].toString());
			}
			// 司机姓名
			if (userDetalObj[14] != null) {
				pushCompetitiveOrder.setDriverName(userDetalObj[14].toString());
			}
			// 车主信誉度
			if (userDetalObj[15] != null) {
				pushCompetitiveOrder.setUserLevel(userDetalObj[15].toString());
			}
		}

	}
}
